package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.UserAddressDAO;
import com.wisewin.circle.entity.db.UserAddressDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserAddressService {

    @Autowired
    private UserAddressDAO userAddressDAO;

    // 查询用户收货地址
    public ResultDTO queryUserAddress(Integer userId){
        UserAddressDO userAddressDO = userAddressDAO.queryUserAddressByUserId(userId);
        return ResultDTOBuilder.success(userAddressDO);
    }

    // 查询收货地址详细信息
    public ResultDTO queryUserAddressDetail(UserAddressDO userAddressDO){
        if(userAddressDO.getAddressId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询收货地址是否存在
        UserAddressDO userAddressOld = userAddressDAO.queryUserAddressById(userAddressDO.getAddressId());
        if(userAddressOld == null){
            return ResultDTOBuilder.failure("1", "当前收货地址不存在", null);
        }
        return ResultDTOBuilder.success(userAddressOld);
    }

    // 修改收货地址
    public ResultDTO updateUserAddress(UserAddressDO userAddressDO){
        if(userAddressDO.getAddressId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询收货地址是否存在
        UserAddressDO userAddressOld = userAddressDAO.queryUserAddressById(userAddressDO.getAddressId());
        if(userAddressOld == null){
            return ResultDTOBuilder.failure("1", "当前收货地址不存在", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getName())){
            return ResultDTOBuilder.failure("1", "请填写姓名", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getGender())){
            return ResultDTOBuilder.failure("1", "请填写性别", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getPhone())){
            return ResultDTOBuilder.failure("1", "请填写手机号码", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getAddress())){
            return ResultDTOBuilder.failure("1", "请填写收货地", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getAddressDetail())){
            return ResultDTOBuilder.failure("1", "请填写门牌号", null);
        }
        // 修改数据
        userAddressDAO.update(userAddressDO);
        return ResultDTOBuilder.success(userAddressDO);
    }

    // 添加收货地址
    public ResultDTO addUserAddress(UserAddressDO userAddressDO){
        if(StringUtils.isEmpty(userAddressDO.getName())){
            return ResultDTOBuilder.failure("1", "请填写姓名", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getGender())){
            return ResultDTOBuilder.failure("1", "请填写性别", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getPhone())){
            return ResultDTOBuilder.failure("1", "请填写手机号码", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getAddress())){
            return ResultDTOBuilder.failure("1", "请填写收货地", null);
        }
        if(StringUtils.isEmpty(userAddressDO.getAddressDetail())){
            return ResultDTOBuilder.failure("1", "请填写门牌号", null);
        }
        // 添加数据
        userAddressDAO.insert(userAddressDO);
        return ResultDTOBuilder.success(userAddressDO);
    }
}
